<template>
  <div>
    <el-dialog
      :before-close="cancel"
      :title="info.isAdd ? '添加角色' : '编辑角色'"
      :visible.sync="info.isShow"
      center
    >
      <el-form ref="role" :model="roleinfo" :rules="rules">
        <el-form-item prop="rolename" label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="roleinfo.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="rolename" label="角色权限" :label-width="formLabelWidth">
          <el-tree
            ref="tree"
            :data="menulist"
            show-checkbox
            node-key="id"
            default-expand-all
            :props="{
              children: 'children',
              label: 'title',
            }"
            check-strictly
          ></el-tree>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="roleinfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的接口
import { roleAdd, roleEdit, roleInfo } from "../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      roleinfo: {
        rolename: "", //角色名称
        menus: "", //字符串 后端要求的是以','分割的字符串类型
        status: 1 //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  props: ["info"],
  mounted() {
    if (this.menulist.length == 0) {
      this.getMenu();
    }
  },
   computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
    methods: {
    ...mapActions({
      getRole: "role/getRoleAction",
      getMenu: "menu/getMenuAction",
    }),
    //封装一个关闭弹框的事件
    cancel() {
      this.$emit("cancel", false);
      //当关闭弹框就调用重置事件
      this.reset();
    },
    //清空输入的表单内容以及相关验证
    reset() {
      this.roleinfo = {
        rolename: "", //角色名称
        menus: "", //字符串
        status: 1, //状态1正常2禁用
      };
      this.$refs.role.resetFields();
      //清空tree节点的选择状态
      this.$refs.tree.setCheckedKeys([]);
    },
    //创建一个查询一条数据事件
    lookup(id) {
      //调用查询一条数据事件
      roleInfo({ id }).then((res) => {
        console.log(res, "一条角色");
        if (res.code == 200) {
          this.roleinfo = res.list;
          this.roleinfo.id = id;
          //给tree节点设置数据 返回的数据格式是字符串，我们要把它转化成数组
          this.$refs.tree.setCheckedKeys(this.roleinfo.menus.split(","));
        }
      });
    },
    //封装一个确定事件
    confirm() {
      //先验证
      this.$refs.role.validate((val) => {
        if (val) {
          // console.log(this.roleinfo);
          // console.log(this.roleinfo);
          /* 
            通过getCheckedKeys方法获取的数据结果是数组格式 [8, 11, 10]
            但是后端要求的是以,分割的字符串格式
            */
          // console.log(this.$refs.tree.getCheckedKeys().join(','),'获取选中的id组成的数组');
          this.roleinfo.menus = this.$refs.tree.getCheckedKeys().join(",");
          //验证成功之后，分别调用添加或者编辑接口
          if (this.info.isAdd) {
            //调用添加接口
            roleAdd(this.roleinfo).then((res) => {
              // console.log(res,'添加的结果');
              if (res.code == 200) {
                this.$message.success(res.msg);
                //关闭弹框
                this.cancel();
                //调用列表接口
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            // 调用编辑接口
            roleEdit(this.roleinfo).then((res) => {
              // console.log(res,'添加的结果');
              if (res.code == 200) {
                this.$message.success(res.msg);
                //关闭弹框
                this.cancel();
                //调用列表接口
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          console.log("验证失败");
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
